package com.kharthick.pwGame.map;

import java.awt.Graphics;
import java.awt.MediaTracker;
import java.util.Iterator;
import java.util.Vector;

import com.kharthick.pwGame.map.tiles.*;

public class Map {

	// map: first one is column, second one is row
	protected Vector<Vector<Tile>> map = null;
	protected MediaTracker tracker = null;
	
	public String NAME;
	public final int TILE_SIZE;
	public int MAX_PLAYERS;
	
	protected int columns;
	protected int rows;
	
	public Vector<Vector<Tile>> getMap(){
		return map;
	}
	
	public void setMap(String name,
			int maxPlayers,
			int columnSize,
			int rowSize){
		NAME = name;
		MAX_PLAYERS = maxPlayers;
		columns = columnSize;
		rows = rowSize;
	}
	
	public Map(String name,
			int maxPlayers,
			int columnSize,
			int rowSize,
			int tileSize,
			MediaTracker tracker) {
		
		NAME = name;
		TILE_SIZE = tileSize;
		MAX_PLAYERS = maxPlayers;
		if(null == tracker)
			System.out.println("null before");
		this.tracker = tracker;
		if(null == this.tracker)
			System.out.println("null after");
		columns = columnSize;
		rows = rowSize;
		
		map = new Vector<Vector<Tile>>(); 
	}
	
	public void draw(Graphics buffer) {
		for(Iterator<Vector<Tile>> it = map.iterator(); it.hasNext(); ) {
			Vector<Tile> row = it.next();
			for(Iterator<Tile> it2 = row.iterator(); it2.hasNext(); ) {
				Tile tile = it2.next();
				tile.draw(buffer);
				System.out.println("actual drawing");
			}
		}
	}
	
	public Tile getTileByScreenPosition(int x, int y) {
		int col = x / TILE_SIZE;
		int row = y / TILE_SIZE;
		
		if( col >= columns || row >= rows ||
				col < 0 || row < 0)
			return null;
		
		return map.get(row).get(col);
	}
	
	public int getColumns() {
		return columns;
	}

	public int getRows() {
		return rows;
	}

	public void addRow(Vector<Tile> row) {
		map.add(row);
	}

	public Tile createTile(String type, Vector<Integer> pos) {
		Tile tile = null;
		if(type.equals("desert")) {
			 tile = new DesertTile(tracker, TILE_SIZE, pos);
		}
		else if(type.equals("forest")) {
			tile = new ForestTile(tracker, TILE_SIZE, pos);
		}
		else if(type.equals("water")) {
			tile = new WaterTile(tracker, TILE_SIZE, pos);
		}
		else if(type.equals("mountain")) {
			tile = new MountainTile(tracker, TILE_SIZE, pos);
		}
		else if(type.equals("grass")) {
			tile = new GrassTile(tracker, TILE_SIZE, pos);
		}
		return tile;
	}
}
